<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="事件名" prop="eventName">
        <el-input
          v-model="queryParams.eventName"
          placeholder="请输入事件名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
         <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:eventmanage:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="eventmanageList"
      row-key="eventId"
      height="500"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="事件编码" prop="eventCode" />
      <el-table-column label="事件名" align="center" prop="eventName" />
      <el-table-column label="快捷排序" align="center" prop="fastSort" />
      <el-table-column label="唯一事件" align="center" prop="isOnly" />
      <el-table-column label="快捷事件" align="center" prop="isQuickly" />
      <el-table-column label="进度条事件" align="center" prop="isProcess" />
      <el-table-column label="模板编号" align="center" prop="template" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="进度条排序" align="center" prop="processSort" />
      <el-table-column label="操作" align="center" class-name="small-padding" width="180px"  fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['manage:eventmanage:edit']">修改</el-button>
          <el-button link type="primary" @click="handleAdd(scope.row)" v-hasPermi="['manage:eventmanage:add']">新增</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['manage:eventmanage:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改事件管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="eventmanageRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="事件编码" prop="eventCode">
          <el-input v-model="form.eventCode" placeholder="请输入事件编码" />
        </el-form-item>
        <el-form-item label="事件名" prop="eventName">
          <el-input v-model="form.eventName" placeholder="请输入事件名" />
        </el-form-item>
        <el-form-item label="组件类型" prop="component">
          <el-input v-model="form.component" placeholder="请输入组件类型" />
        </el-form-item>
        <el-form-item label="模板编号" prop="template">
          <el-input v-model="form.template" placeholder="模板编号" />
        </el-form-item>
        <el-form-item label="事件描述" prop="eventDesc">
          <el-input v-model="form.eventDesc" placeholder="请输入事件描述" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="父级ID" prop="parentId">
          <el-tree-select
            v-model="form.parentId"
            :data="eventmanageOptions"
            :props="{ value: 'eventId', label: 'eventName', children: 'children' }"
            value-key="eventId"
            placeholder="请选择父级ID"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="快捷排序" prop="fastSort">
          <el-input v-model="form.fastSort" placeholder="请输入快捷排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="进度条排序" prop="processSort">
          <el-input v-model="form.processSort" placeholder="请输入进度条排序" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Eventmanage">
import { listEventmanage, getEventmanage, delEventmanage, addEventmanage, updateEventmanage } from "@/api/manage/eventmanage";
import Templateone from './templateone.vue'
const { proxy } = getCurrentInstance();

const eventmanageList = ref([]);
const eventmanageOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(false);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    eventCode: null,
    eventName: null,
    component: null,
    eventDesc: null,
    unit: null,
    parentId: null,
    fastSort: null,
    isOnly: null,
    isQuickly: null,
    isProcess: null,
    template: null,
    status: null,
    processSort: null
  },
  rules: {
    eventId: [
      { required: true, message: "自增ID不能为空", trigger: "blur" }
    ],
    eventName: [
      { required: true, message: "事件名不能为空", trigger: "blur" }
    ],
    parentId: [
      { required: true, message: "父级ID不能为空", trigger: "blur" }
    ],
    fastSort: [
      { required: true, message: "快捷排序不能为空", trigger: "blur" }
    ],
    isOnly: [
      { required: true, message: "是否唯一事件不能为空", trigger: "blur" }
    ],
    isQuickly: [
      { required: true, message: "是否快捷事件不能为空", trigger: "blur" }
    ],
    isProcess: [
      { required: true, message: "是否进度条事件不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "0启用1禁用不能为空", trigger: "change" }
    ],
    delFlag: [
      { required: true, message: "0正常1删除不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询事件管理列表 */
function getList() {
  loading.value = true;
  listEventmanage(queryParams.value).then(response => {
    eventmanageList.value = proxy.handleTree(response.data, "eventId", "parentId");
    loading.value = false;
  });
}

/** 查询事件管理下拉树结构 */
function getTreeselect() {
  listEventmanage().then(response => {
    eventmanageOptions.value = [];
    const data = { eventId: 0, eventName: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "eventId", "parentId");
    eventmanageOptions.value.push(data);
  });
}
	
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    eventId: null,
    eventCode: null,
    eventName: null,
    component: null,
    eventDesc: null,
    unit: null,
    parentId: null,
    fastSort: null,
    isOnly: null,
    isQuickly: null,
    isProcess: null,
    template: null,
    status: null,
    delFlag: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    processSort: null
  };
  proxy.resetForm("eventmanageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.eventId) {
    form.value.parentId = row.eventId;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加事件管理";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.parentId;
  }
  getEventmanage(row.eventId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改事件管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["eventmanageRef"].validate(valid => {
    if (valid) {
      if (form.value.eventId != null) {
        updateEventmanage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addEventmanage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除事件管理编号为"' + row.eventId + '"的数据项？').then(function() {
    return delEventmanage(row.eventId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
